//
//  VCPerson.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/16.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCPerson: VCBase {
    
    private lazy var personItemView:PersonItemView = {
        let ui = PersonItemView()
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        init_data()
        init_event()
    }
}

extension VCPerson {
    
    private func init_event() -> Void {
        
        cirleAddView.complete = {
            self.pushVcHideBottomBar(title: "添加个人报销", vc: VCPersonManger())
        }
        
        personItemView.complete = {person in
            let vc = VCPersonManger()
            vc.personExpenseInfo = person
            self.pushVcHideBottomBar(title: "编辑个人报销", vc: vc)
        }
    }
    
    private func init_data() -> Void {
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(userid, forKey: SP_USERID)
        // 请求
        self.doPost(api: "search/person/expense", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                return
            }
            // 解析数据
            guard let arrs = dict.value(forKey: SP_BODYREQUEST) as? NSArray else {
                return
            }
            // 转化数据
            var temp = [PersonExpenseInfo]()
            for item in arrs {
                let person = PersonExpenseInfo(dict: item as! [String : Any])
                temp.append(person)
            }
            self.personItemView.personExpenses = temp
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(personItemView)
        view.addSubview(cirleAddView)
    }
}
